package com.hanamaki.basic.homewrk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Demo06 {

    public static void main(String[] args) {
        getDay();
    }

    public static void getDay() {
//        实例化Scanner对象
        Scanner scanner = new Scanner(System.in);

        String answer = "是";
        while (answer.equals("是")) {
            System.out.println("请输入年份：");
            int y = scanner.nextInt();
//            判断年份是否满足要求
//            得到今年是哪一年
            String year = new SimpleDateFormat("yyyy").format(new Date());
            if (y <= 0 || y > Integer.parseInt(year)) {
                System.out.println("您输入的年份有误!(年份必须大于0,并且不能超出今年)");
                System.out.println("请重新输入正确的年份：");
                continue;
            }

            System.out.println("请输入月份：");
            int m = scanner.nextInt();

            boolean flag = true;
            while (flag) {
                if (m > 12 || m < 1) {
                    System.out.println("您输入的月份有误!(月份范围为1-12)");
                    System.out.println("请重新输入正确的月份： ");
                    m = scanner.nextInt();
                    continue;
                } else {
                    flag = false;
                }
            }

            int day = 0;
            switch (m) {
                case 2:
                    day = 28;
                    if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
                        day++;
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = 30;
                    break;
                default:
                    day = 31;
            }

            System.out.println(year + "年" + m + "月，一个有" + day + "天");
            System.out.println("是否要再次查询");
            answer = scanner.next();
        }
    }
}
